﻿using BLL;
using Inspinia_MVC5.Code;
using Inspinia_MVC5.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inspinia_MVC5.Controllers
{
    [AllowCors]
    [SessionTimeOutFilter]
    public class MapController : Controller
    {
        // GET: Map
        public ActionResult Index()
        {
            return View();
        }
        //个人所有区块数据
        public JsonResult Block()
        {
            string IP = Request.ServerVariables.Get("Local_Addr").ToString();
            string port = Request.ServerVariables["SERVER_PORT"].ToString();
            BlockService BlockService = new BlockService();
            var blocks = BlockService.All();
            foreach(var block in blocks)
            {
                block.MapPath = "104.219.42.52:" + port + block.MapPath;
            }
            if (Session["identity"].ToString() == "Admin")
            {
                blocks = blocks.OrderBy(c => c.ID);
                return Json(new { status = "0", data = blocks }, JsonRequestBehavior.AllowGet);
            }
            else if (Session["identity"] != null)
            {
                string blockstr = Session["blockstr"].ToString();
                blocks = blocks.Where(c => blockstr.Contains("-" + c.ID.ToString() + "-")).OrderBy(c => c.ID);
                return Json(new { status = "0", data = blocks }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "7" }, JsonRequestBehavior.AllowGet);
            }
        }
        //地图页面单井弹出框数据
        public JsonResult WellData(string wellid)
        {
            R_SIGN_INDDService R_SIGN_INDDService = new R_SIGN_INDDService();
            var welldata = R_SIGN_INDDService.FindAll(c=>c.Well_ID==wellid).OrderByDescending(s=>s.Time).First();
            WellDataModel welldatamodel = new WellDataModel();
            if(welldata != null)
            {
                welldatamodel.WellId = wellid;
                //welldatamodel.Datetime = welldata.Time.ToString("yyyy-MM-dd HH:mm:ss");
                welldatamodel.Datetime = welldata.Time.ToString("MM/dd/yyyy HH:mm:ss");
                welldatamodel.Stroke = (float.Parse(welldata.MW4091) / 1000).ToString();
                welldatamodel.Jig = (float.Parse(welldata.MW4092) / 100).ToString();
                welldatamodel.Up_Jig = (float.Parse(welldata.MW4093) / 100).ToString();
                welldatamodel.Down_Jig = (float.Parse(welldata.MW4094) / 100).ToString();
                welldatamodel.Data_Disp =ResultHelper.s2f(welldata.MW4097 + welldata.MW4107 + welldata.MW4117 + welldata.MW4127 + welldata.MW4137 + welldata.MW4147 + welldata.MW4157 + welldata.MW4167 + welldata.MW4177 + welldata.MW4187 + welldata.MW4197 + welldata.MW4207 + welldata.MW4217 + welldata.MW4227 + welldata.MW4237 + welldata.MW4247 + welldata.MW4257 + welldata.MW4267 + welldata.MW4277 + welldata.MW4287,1000);
                welldatamodel.Data_Load = ResultHelper.s2f(welldata.MW4297 + welldata.MW4307 + welldata.MW4317 + welldata.MW4327 + welldata.MW4337 + welldata.MW4347 + welldata.MW4357 + welldata.MW4367 + welldata.MW4377 + welldata.MW4387 + welldata.MW4397 + welldata.MW4407 + welldata.MW4417 + welldata.MW4427 + welldata.MW4437 + welldata.MW4447 + welldata.MW4457 + welldata.MW4467 + welldata.MW4477 + welldata.MW4487, 100);
                if (Session["identity"].ToString() == "Admin")
                {
                    return Json(new { status = "0", data = welldatamodel }, JsonRequestBehavior.AllowGet);
                }
                else if (Session["identity"] != null)
                {
                    string wellstr = Session["wellstr"].ToString();
                    if (wellstr.Contains(("-" + wellid + "-")))
                    {
                        return Json(new { status = "0", data = welldatamodel }, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
                    }
                }
                else
                {
                    return Json(new { status = "7" }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult ColorPlan(int blockid)
        {
            ColorPlanService ColorPlanService = new ColorPlanService();
            BlockService BlockService = new BlockService();
            var block = BlockService.Find(c => c.ID == blockid);
            var colorplan = ColorPlanService.Find(c => c.ID == block.Plan);

            return Json(new { status = "0", data = colorplan }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult ColorPlanOfWell(int wellid)
        {
            ColorPlanService ColorPlanService = new ColorPlanService();
            BlockService BlockService = new BlockService();
            WellService WellService = new WellService();
            var well = WellService.Find(c => c.ID == wellid);
            var block = BlockService.Find(c => c.ID == well.BLOCK_ID);
            var colorplan = ColorPlanService.Find(c => c.ID == block.Plan);

            return Json(new { status = "0", data = colorplan }, JsonRequestBehavior.AllowGet);
        }
    }
}